package models

import (
	//"fmt"
	"io/ioutil"
	"log"
	"net/http"

	"database/sql"
	"encoding/xml"

	_ "github.com/go-sql-driver/mysql"
)

type Sut_Source struct {
	_Id  int    `json:"id"`
	Type string `json:"type"`
	Url  string `json:"src"`
}
type Sut_live struct {
	Sources []Sut_Source `json:"sources"`
}

func (this *Sut_live) Get_Live_Src() error {
	//取得video.js直播的源地址与直播类型
	var (
		sqlstr string
		rows   *sql.Rows
		source Sut_Source
		err    error
	)
	sqlstr = "select type,url from live_sources where deleted=0"
	rows, err = db.Query(sqlstr)
	if err != nil {
		return err
	}
	for rows.Next() {
		err = rows.Scan(&source.Type, &source.Url)
		this.Sources = append(this.Sources, source)
	}
	return err
}

/*用于分析nginx-rtmp-modules的xls文件的结构体 START*/
type Sut_rtmp_video struct {
	XMLName    xml.Name `xml:"video"`
	Width      string   `xml:"width"`
	Height     string   `xml:"height"`
	Frame_rate string   `xml:"frame_rate"`
	Codec      string   `xml:"codec"`
	Profile    string   `xml:"profile"`
	Compat     string   `xml:"compat"`
	Level      string   `xml:"level"`
}

type Sut_rtmp_audio struct {
	XMLName    xml.Name `xml:"audio"`
	Codec      string   `xml:"codec"`
	Profile    string   `xml:"profile"`
	Channels   string   `xml:"channels"`
	Samplerate string   `xml:"sample_rate"`
}

type Sut_rtmp_meta struct {
	XMLName xml.Name       `xml:"meta"`
	Video   Sut_rtmp_video `xml:"video"`
	Audio   Sut_rtmp_audio `xml:"audio"`
}

type Sut_rtmp_client struct {
	XMLName   xml.Name `xml:"client"`
	Id        string   `xml:"id"`
	Address   string   `xml:"address"`
	Time      string   `xml:"time"`
	Timestamp string   `xml:"timestamp"`
	//Publishing
}

type Sut_rtmp_stream struct {
	XMLName   xml.Name          `xml:"stream"`
	Name      string            `xml:"name"`
	Time      string            `xml:"time"`
	Bw_in     string            `xml:"bw_in"`
	Bytes_in  string            `xml:"bytes_in"`
	Bw_out    string            `xml:"bw_out"`
	Bytes_out string            `xml:"bytes_out"`
	Bw_audio  string            `xml:"bw_audio"`
	Bw_video  string            `xml:"bw_video"`
	Nclients  string            `xml:"nclients"`
	Meta      Sut_rtmp_meta     `xml:"meta"`
	Client    []Sut_rtmp_client `xml:"client"`
}

type Sut_rtmp_live struct {
	XMLName  xml.Name          `xml:"live"`
	Nclients string            `xml:"nclients"`
	Stream   []Sut_rtmp_stream `xml:"stream"`
}

type Sut_rtmp_application struct {
	XMLName xml.Name        `xml:"application"`
	Name    string          `xml:"name"`
	Live    []Sut_rtmp_live `xml:"live"`
}

type Sut_rtmp_server struct {
	XMLName     xml.Name               `xml:"server"`
	Application []Sut_rtmp_application `xml:"application"`
}

type Sut_RTMP struct {
	XMLName       xml.Name        `xml:"rtmp"`
	Nginx_version string          `xml:"nginx_version"`
	Pid           string          `xml:"pid"`
	Uptime        string          `xml:"uptime"`
	Bw_in         string          `xml:"bw_in"`
	Bytes_in      string          `xml:"bytes_in"`
	Bw_out        string          `xml:"bw_out"`
	Bytes_out     string          `xml:"bytes_out"`
	Server        Sut_rtmp_server `xml:"server"`
}

/*用于分析nginx-rtmp-modules的xls文件的结构体 END*/
func init() {
	//init
}

//分析nginx-rtmp-modules的xls，将xml转换成结构体
func (this *Sut_RTMP) GetServerState() {
	var (
		//rtmp Sut_RTMP
		err error
	)
	resp, err := http.Get("http://c.kjdll.com:8888/stat")
	if err != nil {
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body) //body为URL返回的正文，如果是XML即为源XML
	if err != nil {
		// handle error
		return
	}
	log.Println(string(body)) //LOG，XML内容
	//解析xml内容
	//rtmp = Sut_RTMP{}
	err = xml.Unmarshal(body, this)
	if err != nil {
		log.Println("解析xml出错！")
		return
	}
	//log.Println("uptime:", this.Uptime)
	//log.Println(this)
	//log.Println("uptime:", result.Uptime)
}

/*
type Application struct {
	Name string `xml:"app_name,attr"`
	Data string `xml:"app_data,attr"`
}
type AppLog struct {
	Applications []Application `xml:"application"`
}
type CDR struct {
	XMLName xml.Name `xml:"cdr"`
	AppLog  AppLog   `xml:"app_log"`
}

//XML解析示例
func (this *Sut_live) Test1() {
	cdr := CDR{}
	data := `
    <?xml version="1.0" encoding="utf-8"?>
    <cdr>
     <app_log>
        <application app_name="set" app_data="aaa"></application>
        <application app_name="set" app_data="bbb"></application>
      </app_log>
    </cdr>
    `
	err := xml.Unmarshal([]byte(data), &cdr)
	if err != nil {
		panic(err)
		return
	}
	fmt.Println(cdr.AppLog)
}
*/
